package kr.co.redcircle.service.oAuth.api.facebook;

import kr.co.redcircle.service.oAuth.model.AbstractCommand;
import kr.co.redcircle.service.oAuth.model.ISNSCommandParameters;
import kr.co.redcircle.service.oAuth.model.ISNSResult;
import kr.co.redcircle.service.oAuth.model.SNSDefaultParameters;
import kr.co.redcircle.service.oAuth.model.SNSDefaultResult;

import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;

public class CommandPostThread extends AbstractCommand {

	@Override
	public ISNSResult invoke(ISNSCommandParameters paramCollection) {

		System.out.println("CommandPostThread.invoke.define value");
		SNSDefaultResult result = new SNSDefaultResult();
		SNSDefaultParameters params = (SNSDefaultParameters)paramCollection;
		Token accessToken = params.getAccessToken();
		OAuthService service = params.getService();
		String messageString  = (String) params.get("message");
		Verb type = getVerb(this.getKeyMethod().toLowerCase().trim());

		
		System.out.println("CommandPostThread.invoke.define request object : " + type);
		OAuthRequest req = new OAuthRequest(type, this.getKeyVALUE());		
		req.addBodyParameter("message", messageString);

		System.out.println("CommandPostThread.invoke.signing request" + accessToken + " , " + messageString);
		service.signRequest(accessToken, req);
		
		System.out.println("CommandPostThread.invoke.send request~" + req);
		Response resp = req.send();
		
		System.out.println("CommandPostThread.invoke.response");
		result.setCode(resp.getCode()+"");
		result.setBody(resp.getBody());
		result.setHeaders(resp.getHeaders());
		
		System.out.println("result.code : " + result.getCode());
		System.out.println("result.body : " + result.getBody());
		
		return result;
		
	}

}
